import sha

sha = sha.new

def hashSha512(password, salt='', n=12):
    from hashlib import sha512
    salt =  salt or sha(urandom(40)).hexdigest()
    hashed = sha512(salt + password).hexdigest()
    for i in xrange(n):
        hashed = sha512(hashed + salt).hexdigest()
    return '$sha512$%i$%s$%s' % (n, salt, hashed)

def hashSha1(password, salt='', n=12):
    salt =  salt or sha(urandom(32)).hexdigest()
    hashed = sha(salt + password).hexdigest()
    for i in xrange(n):
        hashed = sha(hashed + salt).hexdigest()
    return '$sha1$%i$%s$%s' % (n, salt, hashed)

def hashBcrypt(password, salt='', n=12):
    import bcrypt
    salt.replace('$bcrypt$', '$2a$', 1)
    salt = salt or bcrypt.gensalt(n)
    hashed = bcrypt.hashpw(password, salt)
    return hashed.replace('$2a$', '$bcrypt$', 1)